<?php

/**
 * Description of DB_MySQL
 *
 * @since 1.0 <2015-8-25> SoChishun Added.
 */
class DB_MySQL {

    protected $link;

    /**
     * 连接到数据库
     * @param 数据库配置 $config
     * @param string $msg
     * @return resource|null
     * @since 1.0 <2015-8-25> SoChishun Added.
     */
    public function connect($config, &$msg = '') {
        try {
            $this->link = mysql_connect($config['DB_HOST'], $config['DB_USER'], $config['DB_PASSWORD']);
            if (!empty($config['DB_CHARSET'])) {
                mysql_query('set names ' . $config['DB_CHARSET']);
            }
            if (!empty($config['DB_NAME'])) {
                mysql_select_db($config['DB_NAME']);
            }
            return $this->link;
        } catch (Exception $ex) {
            $msg = $ex->getMessage();
        }
        return null;
    }

    public function query_array_list($sql) {
        $res = mysql_query($sql);
        if (!$res) {
            return $res;
        }
        while ($row = mysql_fetch_assoc($res)) {
            $out[] = $row;
        }
        return $out;
    }
    
    public function query_array_row($sql){
        $res = mysql_query($sql);
        if (!$res) {
            return $res;
        }
        while ($row = mysql_fetch_assoc($res)) {
            return $row;
        }
        return false;        
    }

}
